<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学号" prop="number">
      <el-input v-model="dataForm.number" placeholder="学号"></el-input>
    </el-form-item>
    <el-form-item label="学院" prop="college">
      <el-input v-model="dataForm.college" placeholder="学院"></el-input>
    </el-form-item>
    <el-form-item label="班级" prop="class">
      <el-input v-model="dataForm.class" placeholder="班级"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="是否为队长" prop="statu">
      <el-input v-model="dataForm.statu" placeholder="是否为队长"></el-input>
    </el-form-item>
    <el-form-item label="项目id" prop="teamId">
      <el-input v-model="dataForm.teamId" placeholder="项目id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifiedTime">
      <el-input v-model="dataForm.modifiedTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          number: '',
          college: '',
          class: '',
          name: '',
          sex: '',
          phone: '',
          statu: '',
          teamId: '',
          createTime: '',
          modifiedTime: ''
        },
        dataRule: {
          number: [
            { required: true, message: '学号不能为空', trigger: 'blur' }
          ],
          college: [
            { required: true, message: '学院不能为空', trigger: 'blur' }
          ],
          class: [
            { required: true, message: '班级不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          statu: [
            { required: true, message: '是否为队长不能为空', trigger: 'blur' }
          ],
          teamId: [
            { required: true, message: '项目id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifiedTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/student/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.number = data.student.number
                this.dataForm.college = data.student.college
                this.dataForm.class = data.student.class
                this.dataForm.name = data.student.name
                this.dataForm.sex = data.student.sex
                this.dataForm.phone = data.student.phone
                this.dataForm.statu = data.student.statu
                this.dataForm.teamId = data.student.teamId
                this.dataForm.createTime = data.student.createTime
                this.dataForm.modifiedTime = data.student.modifiedTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/student/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'number': this.dataForm.number,
                'college': this.dataForm.college,
                'class': this.dataForm.class,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'phone': this.dataForm.phone,
                'statu': this.dataForm.statu,
                'teamId': this.dataForm.teamId,
                'createTime': this.dataForm.createTime,
                'modifiedTime': this.dataForm.modifiedTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
